//
//  ViewController.swift
//  XHPromise
//
//  Created by xinph on 2020/4/13.
//  Copyright © 2020 xinph. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        /// eg1
        self.callPromise()

        /// eg2
        Promise<Int, Any>(value: 2)
            .then { (res) in
                print(res)
        }
        
        /// eg2
        Promise<Int, String>(error: "❎")
            .catch { (err) in
                print(err)
        }
    }
    
    
    func callPromise() {
        Promise { (resolve, reject) in
            print("即将开启一个异步操作")
            DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
                if arc4random()%2 == 0 {
                    resolve("执行完毕")
                } else {
                    reject("执行错误")
                }
            }
        }.then { (res) in
            print(res)
        }.catch { (err) in
            print(err)
        }
    }
}

